package com.sh.suhe.utildemos.views.activity.photo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.bumptech.glide.Glide;
import com.sh.suhe.utildemos.R;
import com.sh.suhe.utildemos.utils.LogUtil;
import com.sh.suhe.utildemos.utils.http.NetUtil;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.HashMap;

public class PaPhotoDetailActivity extends AppCompatActivity {
    private static final String TAG= PaPhotoDetailActivity.class.getSimpleName();
    ZoomImageView zoomImageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pa_photo_detail);
        zoomImageView = findViewById(R.id.pa_detail_iv);
        Bundle bundle = getIntent().getExtras();
        String url = bundle == null ? "" : bundle.getString("url");
        if (PaPhotoActivity.type == 1) {
            loadUrl(url);
        }else if (PaPhotoActivity.type == 2) {
            loadImg(url);
        }
        Log.e(TAG, url);
    }
    private void loadUrl(String url) {
        LogUtil.e(TAG, url);
        NetUtil.request(NetUtil.RequestMethod.GET, url, new HashMap<>(), new NetUtil.RequestCallBack() {
            @Override
            public void onSuccess(int statusCode, String json) {
                //mt10 feilei
                Document doc = Jsoup.parse(json);
                //获取所有的博客列表集合
                Elements blogList = doc.select("a[class=image_gall]");
                if (blogList != null && blogList.size() > 0){
                   String href = blogList.get(0).attr("href");
                    if (!TextUtils.isEmpty(href)) {
                        loadImg("https:" + href);
                    }
                }

            }

            @Override
            public void onFailure(int statusCode, String errorMsg) {
                LogUtil.e(TAG,errorMsg);
            }
        });
    }
    private void loadImg(String path) {
        if (!TextUtils.isEmpty(path)) {
            Glide.with(PaPhotoDetailActivity.this).load(path)
                    .error(R.mipmap.ic_launcher).into(zoomImageView);
        }
    }
}